import gameObject, agents, graphics, math, sys, time, pygame
from tank import Tank

class Game( graphics.Graphics ):
    """
    GameState is a concrete class holding a game state and initiating the first one.
    """
    def __init__(self):
        # Start PyGame and initiate graphics.
        pygame.init()
        graphics.Graphics.__init__(self)        
        pygame.display.set_caption('Think Tanks')
        pygame.display.flip()
        # Register the object groups.
        self.highObstacles = gameObject.ObjectGroup() # Collides with tanks + bullets.
        self.lowObstacles = gameObject.ObjectGroup() # Collides with bullets only.
        self.tanks = gameObject.ObjectGroup()
        self.bullets = gameObject.ObjectGroup()
        self.explosions = gameObject.ObjectGroup()
        # For automatic clearing, updating and drawing, add all groups to groupList.
        self.groupList = []
        self.groupList.append(self.highObstacles)
        self.groupList.append(self.lowObstacles)
        self.groupList.append(self.tanks)
        self.groupList.append(self.bullets)
        self.groupList.append(self.explosions)
        # Create some objects.
        self.tankGreen = Tank((100,100), 100, 1.0, self, agentName="KeyboardAgent", team="green")
        self.tankRed1 = Tank((300,300), 100, math.pi/2, self, agentName="IdleAgent", team="red")
        self.tankRed2 = Tank((400,400), 100, math.pi/6, self, agentName="IdleAgent", team="red")
        self.tankRed3 = Tank((500,500), 100, -5*math.pi/4, self, agentName="IdleAgent", team="red")
        self.tanks.add(self.tankGreen, self.tankRed1, self.tankRed2, self.tankRed3)

    def computeFPS(self, frame, firstTime):
        """
        Draws current FPS in bottom-right corner of display.
        """
        fps = frame / (time.time() - firstTime)
        fpsFont = pygame.font.Font(None, 40)
        fpsText = fpsFont.render(str(round(fps)), 1, (10, 10, 10))
        fpsRectangle = fpsText.get_rect()
        fpsRectangle.bottomright = (self.display.get_width() - 30, self.display.get_height() - 30)
        clearRectangle = pygame.Rect(0, 0, 150, 80)
        clearRectangle.bottomright = (self.display.get_width(), self.display.get_height())
        self.display.blit(self.background, clearRectangle, clearRectangle)
        self.display.blit(fpsText, fpsRectangle)

    def run(self):
        """
        Main control loop.
        """
        previousTime = time.time()
        firstTime = previousTime
        frame = 0
        while True:
            # Easy escape!
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
            # Set the start time.
            frame += 1
            currentTime = time.time()

            for g in self.groupList:
                g.update(previousTime, currentTime)
                
            # Compute and blit the FPS.
            self.computeFPS(frame, firstTime)
            # Update the screen and change the time.
            pygame.display.flip()
            previousTime = currentTime
           
newgame = Game()
newgame.run()
